from sqlalchemy import Column, Integer, String, Sequence, or_
from sqlalchemy.orm import Session

from app.db import db_query, db_update, Base


class TagGroup(Base):
    """
    标签分组表
    """
    # ID
    id = Column(Integer, Sequence('id'), primary_key=True, index=True)
    # 分组名
    name = Column(String)
    # 分组名(显示)
    name_display = Column(String)

    @staticmethod
    @db_query
    def get_by_name(db: Session, name: str):
        return db.query(TagGroup).filter(or_(TagGroup.name.__eq__(name), TagGroup.name_display.__eq__(name))).first()

    @db_update
    def delete_by_name(self, db: Session, name: str):
        user = self.get_by_name(db, name)
        if user:
            user.delete(db, user.id)
        return True
